package com.common.base.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import sun.awt.AppContext;

import com.common.base.dao.BaseDao;
import com.common.base.entity.BaseEntity;
import com.common.base.exceptions.DaoException;
import com.common.tag.Page;

public class BaseDaoImpl<T extends BaseEntity> extends SqlMapClientDaoSupport
		implements BaseDao<T> {
	
	protected Class<T> entityClass; // model's Class
	protected String entityClassName; // model's ClassName
	
	protected String statement_Find;
	protected String statement_Insert;
	protected String statement_Delete;
	protected String statement_Update;
	
	
//	private SqlExecutor sqlExecutor;
//
//	public SqlExecutor getSqlExecutor() {
//		return sqlExecutor;
//	}
//
//	public void setSqlExecutor(SqlExecutor sqlExecutor) {
//		this.sqlExecutor = sqlExecutor;
//	}

	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {

		Object superClassType = getClass().getGenericSuperclass();

		if (superClassType instanceof ParameterizedType) {
			entityClass = (Class<T>) ((ParameterizedType) getClass()
					.getGenericSuperclass()).getActualTypeArguments()[0];
			entityClassName = entityClass.getSimpleName();
			
			statement_Insert = entityClassName + _INSERT;
			statement_Delete = entityClassName + _DELETE;
			statement_Update = entityClassName + _UPDATE;
			statement_Find = entityClassName + _FIND;
		}
	}

	 

	
	// 通用方法
	// 1、insert method
	public BigDecimal insert(T entity) throws DaoException {
		
		BigDecimal sid = new BigDecimal(-1);
		try {
			sid = (BigDecimal) this.getSqlMapClientTemplate().insert(statement_Insert, entity);
		} catch (RuntimeException e) {
			throw  new DaoException("UD010000", new String[]{entityClass.getName()},e);
		}
		
		return sid;
	}
	
	@SuppressWarnings("unchecked")
	public final List<T> findByParams(Map<String, Object> params) {	
		if(params==null){
			params=new HashMap<String, Object>();
		}
		List<T> lists = this.getSqlMapClientTemplate().queryForList(statement_Find, params);
		return lists;
	}

	
	public final int update(T entity) {
		entity.setUpdatedDate(new Date());
		int i= this.getSqlMapClientTemplate().update(statement_Update, entity);
		return i;
	}

	public int delete(T entity) {
		int i= this.getSqlMapClientTemplate().delete(statement_Delete, entity);
		return i;		
	}
	
	@SuppressWarnings("unchecked")
	public T findBySid(Long sid){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("sid", sid);
		List<T> midList = findByParams(params);
		if(null != midList && midList.size() == 1)
		{
			return midList.get(0);
		}else{//待 完善呐
			return null;
		}
	}
	
	
	
/*************************下面的方法还没完善********************************************/






	// 5、查询page页面
	@SuppressWarnings("unchecked")
	public final List<T> findPageByParams(int first, int max) {
		// Session session = super.getSession();
		//		
		// Query query = session.createQuery(queryString);
		// query.setFirstResult(first);
		// query.setMaxResults(max);
		//		
		// session.close();
		//		
		// List<T> entities = query.list();

		return null;
	}

	// 6、查询该sid的对象是否存�?
	@SuppressWarnings("unchecked")
	public final Boolean isExsit(Long sid) {
		// Session session = super.getSession();
		// String hql= queryString + "where sid =:sid";
		// Query query=session.createQuery(hql);
		// query.setLong("sid", sid);
		//		
		// List<T> list =null;
		// try{
		// list = query.list();
		// } catch (HibernateException e){
		// e.printStackTrace();
		// }
		//		
		// if(list.isEmpty() || null == list){
		// return false;
		// }else{
		return true;
		// }
	}

	// 7、�?过Params查询list页面


	public T findEntityBySid(Long sid) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 分页查询
	 * @curPage 当前页码
	 * @pageSize 每页显示的记录条数
	 * */
	public Page<T> findByPage(Map<String, Object> params, int pageNo, int limit) {
		
		params.put(BaseDao.GLOBAL_FACILITY_KEY, "FACILITY");
		Page<T> page = new Page<T>();
		page.setPageNo(pageNo);
		page.setLimit(limit);

		try {
			List<T> resultList = getSqlMapClientTemplate().queryForList(
					statement_Find, params, page.getOffset(), limit);
			page.setResultList(resultList);
		} catch (DataAccessException e) {
			throw new DaoException("jdbc.error.code.Common.findByPage", new String[]{entityClass.getName()},e);
		}
		// page.setTotalRows(((LimitSqlExecutor) getSqlExecutor()).getTotalRowsCount());
		return page;
	}




	public Boolean isExsit(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}



}
